package com.iscreen.core.trading.actions.flows;

import java.util.Date;
import java.util.List;

import com.iscreen.core.services.downloader.DownloadMessage;
import com.iscreen.core.services.extractor.ExtractMessage;
import com.iscreen.core.services.flow.Flow;
import com.iscreen.core.services.flow.FlowType;
import com.iscreen.core.services.flow.StepType;
import com.iscreen.core.services.parameters.ParameterConstants;
import com.iscreen.core.services.parameters.ParametersHandler;
import com.iscreen.core.services.parameters.ParametersHandlerBean;
import com.iscreen.core.services.parser.ParseMessage;
import com.iscreen.core.services.parser.ParserType;
import com.iscreen.core.utils.ejb.ServiceLocator;
import com.iscreen.utils.constants.Constants;
import com.iscreen.utils.dates.DateUtils;
import com.iscreen.utils.exceptions.IScreenException;

public class FlowImportDailyData extends Flow
{
	public FlowImportDailyData()
	{
		super(FlowType.IMPORT_DAILY_DATA);
	}

	@Override
	protected void startFlow(Object... params) throws IScreenException
	{
		download(params[0]);
	}

	@Override
	protected void proceed(StepType currentStep, Object stepResult) throws IScreenException
	{
		switch (currentStep)
		{
			case DOWNLOAD:
				extract(stepResult);
				break;
			case EXTRACT:
				parse(stepResult);
				break;
			case PARSE:
				break;
		}
	}
	
	private void download(Object dateObj) throws IScreenException
	{
		Date date = (Date) dateObj;
		DownloadMessage downloadMessage = createDownloadMessage(date);
		sendMessage(StepType.DOWNLOAD, downloadMessage);
	}

	private DownloadMessage createDownloadMessage(Date date) throws IScreenException
	{
		ParametersHandler parametersHandler = ServiceLocator.getInstance().getEJB(ParametersHandler.class);
		String dataFileUrlPattern = parametersHandler.getParameterValue(ParameterConstants.SECTION_DATA_IMPORT, ParameterConstants.PARAM_TASE_DAILY_FILE_DISTRIBUTION_URL_PATTERN);
		String dateStr = DateUtils.formatDate(date);
		String dataFileUrl = String.format(dataFileUrlPattern, dateStr);
		String targetFolder = parametersHandler.getParameterValue(ParameterConstants.SECTION_DATA_IMPORT, ParameterConstants.PARAM_ZIPPED_DAILY_DATA_FOLDER);
		return new DownloadMessage(dataFileUrl,targetFolder + Constants.FILE_SEPARATOR  + dateStr + ".zip");
	}
	
	private void extract(Object zipFileObj) throws IScreenException
	{
		ParametersHandler parametersHandler = ServiceLocator.getInstance().getEJB(ParametersHandler.class);
		String zipFile = (String) zipFileObj;
		String targetFolder = parametersHandler.getParameterValue(ParameterConstants.SECTION_DATA_IMPORT, ParameterConstants.PARAM_EXTRACTED_DAILY_DATA_FOLDER);
		ExtractMessage extractMessage = new ExtractMessage(zipFile, targetFolder);
		sendMessage(StepType.EXTRACT, extractMessage);
	}
	
	@SuppressWarnings("unchecked")
	private void parse(Object dataFileNamesObj) throws IScreenException
	{
		List<String> dataFileNames = (List<String>) dataFileNamesObj;
		for (String dataFileName : dataFileNames)
		{
			ParseMessage parseMessage = new ParseMessage(ParserType.TRADING_DAILY_DATA,dataFileName);
			sendMessage(StepType.PARSE, parseMessage);
		}
	}

}
